package com.hushao.lims.weblims.controller;

import com.hushao.lims.weblims.domain.EquipmentScrap;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.service.EquipmentScrapService;
import com.hushao.lims.weblims.vo.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 930588706
 * @date 18/1/28
 */
@Controller
@RequestMapping("/equipmentScrap")
public class EquipmentScrapController {
    @Autowired
    private EquipmentScrapService equipmentScrappedService;
    @Autowired
    private UserDetailsService userDetailsService;

    /**
     * 查询器材报废
     * @param equipmentName
     * @param state
     * @param async
     * @param pageable
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "器材报废列表（分页）")
    @PreAuthorize("hasAuthority('ROLE_主管')")
    @GetMapping
    public String listEquipmentScrapped(@RequestParam(value = "equipmentName",required = false,defaultValue = "") String equipmentName,
                                        @RequestParam(value = "state",required = false,defaultValue = "") String state,
                                        @RequestParam(value = "async",required = false) boolean async,
                                        Pageable pageable,
                                        Model model)throws Exception{
        Page<EquipmentScrap> equipmentScrappedPage=equipmentScrappedService.listEquipmentScrappedByEquipmentNameLikeAndStateLike(equipmentName,state,pageable);
        List<EquipmentScrap> equipmentScraps=equipmentScrappedPage.getContent();
        model.addAttribute("page",equipmentScrappedPage);
        model.addAttribute("equipmentScraps",equipmentScraps);
        return async==true?"/audit/equipmentScrapList ::#mainContainerRepleace":"/audit/equipmentScrapList";
    }

    /**
     * 根据名称模糊查询个人报废器材列表
     * @param teacherNumber
     * @param equipmentName
     * @param async
     * @param pageable
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value="个人器材报废列表")
    @GetMapping("/{teacherNumber}")
    public String listEquipmentScrappedByTeacher(@PathVariable("teacherNumber") String teacherNumber,
                                                 @RequestParam(value = "equipmentName",required = false,defaultValue = "") String equipmentName,
                                                 @RequestParam(value = "async",required = false) boolean async,
                                                 Pageable pageable,
                                                 Model model)throws Exception{
        TeacherInfo teacher= (TeacherInfo) userDetailsService.loadUserByUsername(teacherNumber);
        Page<EquipmentScrap> equipmentScrappedPage=equipmentScrappedService.listEquipmentScrappedByTeacherAndEquipmentName(equipmentName,teacher,pageable);
        List<EquipmentScrap> equipmentScraps=equipmentScrappedPage.getContent();
        model.addAttribute("page",equipmentScrappedPage);
        model.addAttribute("equipmentScraps",equipmentScraps);
        return async==true?"/equipments/scrapList ::#mainContainerRepleace":"/equipments/scrapList";
    }

    @ResponseBody
    @DeleteMapping("/{teacherNumber}/revocation/{equipmentScrapId}")
    @PreAuthorize("authentication.name.equals(#teacherNumber)")
    public ResponseData revocation(@PathVariable("equipmentScrapId") Long equipmentScrapId,
                                   @PathVariable("teacherNumber") String teacherNumber)throws Exception{
        equipmentScrappedService.remove(equipmentScrapId);
        return new ResponseData(true,"撤销成功！");
    }
}
